<template>
	<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="circle" class="echarts" />
</template>

<script>
	export default {
		name: "CircleEcharts",
		props: {
			option: Object
		},
		data() {
			return {
				
			};
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		data() {
			return {
				optionEntity: {
					angleAxis: {
						max: 100, // 满分
						clockwise: true, // 逆时针
				
						axisLine: {
							show: false
						},
						axisTick: {
							show: false
						},
						axisLabel: {
							show: false
						},
						splitLine: {
							show: false
						}
					},
					radiusAxis: {
						type: 'category',
				
						axisLine: {
							show: false
						},
						axisTick: {
							show: false
						},
						axisLabel: {
							show: false
						},
						splitLine: {
							show: false
						}
					},
					polar: {
						center: ['50%', '50%'],
						radius: '165%'
					},
					series: [{
						// 灰色环
						type: 'bar',
						data: [{
							value: 100,
							itemStyle: {
								color: '#EEE1D6',
							}
						}],
						coordinateSystem: 'polar',
						roundCap: true,
						barWidth: 10,
						barGap: '-100%', // 两环重叠
						z: 1
					}]
				}
			}
		},
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				console.log('this.entity.', this.option);
				this.optionEntity.angleAxis.max = this.option.totalScore
				this.optionEntity.series.unshift({ 
					type: 'bar',
					data: [{
						name: '',
						value: this.option.score,
						itemStyle: {
							normal: {
								color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
										offset: 0,
										color: '#FF2615'
									},
									{
										offset: 1,
										color: '#FFAA2F'
									}
								])
							}
						}
					}],
					coordinateSystem: 'polar',
					roundCap: true,
					barWidth: 10,
					barGap: '-100%', // 两环重叠
					z: 2
				})
				myChart = echarts.init(document.getElementById('circle'))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.optionEntity)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			/**
			 * Echarts 视图点击事件
			 */
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				// ownerInstance.callMethod('onViewClick', {
				// 	test: 'test'
				// })
			}
		}
	}
</script>

<style scoped lang="scss">
	.echarts {
		width: 100%;
		height: 100%;
	}
</style>